#include "DrawArea.h"
#include <QPainter>
#include <QMouseEvent>
#include <cmath>

DrawArea::DrawArea(QWidget *parent) : QWidget(parent), pendulum(nullptr), isDragging(false) {
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 创建定时器
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(updatePendulum()));
        timer->start(static_cast<int>(dt * 1000)); // 将毫秒转换为秒

        // 创建参数显示标签
           parameterLabel = new QLabel(this);
           parameterLabel->setGeometry(10, 10, 200, 50);

           // 创建用于实时更新信息标签的定时器
           infoLabelTimer = new QTimer(this);
           connect(infoLabelTimer, SIGNAL(timeout()), this, SLOT(updateInfoLabel()));
           infoLabelTimer->start(1); // 设置为每1ms更新一次
}

void DrawArea::setPendulum(InvertedPendulum *pendulum) {
    this->pendulum = pendulum;
}

void DrawArea::setControlLoopInterval(double interval) {
    dt = interval;
    // 停止定时器
    timer->stop();
    // 设置新的时间步并重新启动定时器
    timer->start(static_cast<int>(dt * 1000));
}

// 构造函数和其他函数的实现

void DrawArea::updatePendulum() {
    if (pendulum) {
        // 更新倒立摆状态
        pendulum->ControlLoop();
        // 重新绘制
        update();
    }
}


void DrawArea::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        dragStartPos = event->pos();
    }
}

void DrawArea::mouseMoveEvent(QMouseEvent *event) {
    if (isDragging && pendulum) {
        QPointF delta = event->pos() - dragStartPos;
        QPointF disturbance = delta / 10.0; // 调整系数以控制扰动大小
        pendulum->ApplyDisturbance(dragStartPos.x(),dragStartPos.y(), disturbance.x(),disturbance.y()); // 在 Pendulum 类中实现 ApplyDisturbance 函数
        pendulum->ControlLoop(); // 更新倒立摆状态
        update(); // 重新绘制
        dragStartPos = event->pos(); // 更新拖动起始位置
    }
}

void DrawArea::mouseReleaseEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
    }
}

void DrawArea::paintEvent(QPaintEvent *event) {
    if (pendulum) {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 获取绘图区域的大小
        QSize size = this->size();
        int width = size.width();
        int height = size.height();

        // 绘制底部滑动部分
        painter.setBrush(Qt::gray);
        painter.drawRect(0, height - 100, width, 100);

        // 绘制小车
        double cartX = pendulum->GetPosition() * (width - 100) + width / 2;
        double cartY = height-100; // 底部滑动部分中心的垂直位置
        double cartWidth = 50;
        double cartHeight = 20;

        painter.setPen(Qt::black);
        painter.setBrush(Qt::blue);
        painter.drawRect(cartX - cartWidth / 2, height - cartHeight - 100, cartWidth, cartHeight);

        painter.setPen(Qt::yellow);
        painter.setBrush(Qt::green);
        // 绘制两个圆形
        painter.drawEllipse(QPointF(cartX - cartWidth / 4, cartY), cartWidth / 5, cartHeight / 2);
        painter.drawEllipse(QPointF(cartX + cartWidth / 4, cartY), cartWidth / 5, cartHeight / 2);

        // 绘制倒立摆杆
        double pendulumX = cartX + std::sin(pendulum->GetAngle()) * 50;
        double pendulumY = height - cartHeight - 100 - std::cos(pendulum->GetAngle()) * 50;

        painter.setPen(Qt::black);
        painter.drawLine(cartX, height - cartHeight / 2 - 100, pendulumX, pendulumY);
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPointF(pendulumX, pendulumY), 10, 10);

        // 显示控制变量或其他信息
        // ...
    }
}
void DrawArea::updateInfoLabel() {
    if (pendulum) {
        // 获取倒立摆的参数
        double angle = pendulum->GetAngle();
        double position = pendulum->GetPosition();

        // 将参数转换为字符串
        QString parameterText = QString("Angle: %1\nPosition: %2\n")
            .arg(round(angle)*10/10)
            .arg(round(position)*10/10);

        // 在标签上显示参数
        parameterLabel->setText(parameterText);
    }
}

QSize DrawArea::sizeHint() const {
    return QSize(800, 400); // 设置绘图区域的默认大小
}
